package com.base.dao.impl;

import java.io.Serializable;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import com.base.dao.BaseDao;
import com.utills.ReflectUtil;

@Component
public class BaseDaoImpl<T>
	extends HibernateDaoSupport implements BaseDao<T> {

	//进行持久化操作的hibernateTemplate
	protected HibernateTemplate template=null;
	
	@SuppressWarnings({"unchecked" })
	private Class<T> entityClass=ReflectUtil.getEntity(this.getClass());
	//设值注入SessionFactory必需的 setter方法
	@Autowired
	public void setSessionFactoryUtil(SessionFactory sessionFactory){
		this.setSessionFactory(sessionFactory);
		template=this.getHibernateTemplate();
	}
	
	/**保存特定实例数据*/
	public void save(T entity) {
		template.save(entity);
	}

	/**更新特定实例数据*/
	public void update(T entity) {
		template.update(entity);
	}

	/**删除特定实例数据*/
	public void delete(Serializable... ids) {
		for(Serializable id :ids)
			template.delete(find(id));
	}

	/**查询特定实例数据 */
	public T find(Serializable id) {
		return template.get(entityClass, id);
	}

}
